package com.trailwalker.JavaUtils.log;

import com.trailwalker.JavaUtils.string.StringUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author Trailwalker
 * @create 2020-08-04 20:24
 * @des
 */
public class LogUtils {
    private static boolean isPrint = true;
    private static boolean isPrintAtLocal = true;

    /**
     * 打印日志
     *
     * @param tag
     * @param content
     */
    public static void log(String tag, String content) {
        if (isPrint) {
            System.out.println(tag + " Trailwalker >> " + content);
        }
        if (isPrintAtLocal) {
            putLocalLog(tag + " Trailwalker >> " + content, null, null);
        }
    }

    /**
     * 打印日志(Trailwalker)
     *
     * @param tag
     * @param content
     */
    public static void logT(String tag, String content) {
        if (isPrint) {
            System.out.println(tag + " Trailwalker >> " + content);
        }
        if (isPrintAtLocal) {
            String savePath = System.getProperty("user.dir") + "/log/debug";
            putLocalLog(tag + " Trailwalker >> " + content, savePath, null);
        }
    }

    /**
     * 打印日志(Error)
     *
     * @param e
     */
    public static void logE(Throwable e) {
        if (isPrintAtLocal) {
            String savePath = System.getProperty("user.dir") + "/log/debug";
            putLocalLog("Error >> " + e.toString(), savePath, null);
            StackTraceElement[] stackTrace = e.getStackTrace();
            for (int i = 0; i < stackTrace.length; i++) {
                StackTraceElement stackTraceElement = stackTrace[i];
                putLocalLog("\tat " + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName() + "(" + (stackTraceElement.isNativeMethod() ? "Native Method" : stackTraceElement.getFileName() + ":" + stackTraceElement.getLineNumber()) + ")", savePath, null);
            }
        }
    }

    /**
     * 添加本地日志
     *
     * @param log
     * @param savePath
     * @param fileName
     */
    public static void putLocalLog(String log, String savePath, String fileName) {
        LocalDateTime localDateTime = LocalDateTime.now();
        String date = DateTimeFormatter.ofPattern("yy-MM-dd HH:mm:ss").format(localDateTime);
        String day = DateTimeFormatter.ofPattern("yy-MM-dd").format(localDateTime);
        String time = DateTimeFormatter.ofPattern("HH").format(localDateTime);
        if (StringUtils.isEmpty(savePath)) {
            savePath = System.getProperty("user.dir") + "/log/" + day;
        }
        if (StringUtils.isEmpty(fileName)) {
            fileName = day + " " + time + "h.txt";
        }
        //文件保存位置
        File saveDir = new File(savePath);
        if (!saveDir.exists()) {
            saveDir.mkdirs();
        }
        File file = new File(savePath + File.separator + fileName);
        FileWriter fileWritter;
        try {
            fileWritter = new FileWriter(file, true);
            fileWritter.write(date + " " + log + "\n");
            fileWritter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
